package com.fingard.dsp.bank.directbank.calyon01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description TODO
 * @Author xtt
 * @Date 2019-10-30$ 11:07$
 **/
public class CALYON01Balance extends CALYON01Base implements IBalance {
    /**
     * 今日余额查询 MT942
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
    //创建返回对象
    RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
    //写返回ATS的统一头部信息
        retAts.writeStart();
        while(p_req7506.readRecord()){
        //创建返回到ATS余额对象
        RetAtsBalDetail detail = new RetAtsBalDetail();
        //获取账号
        String accNum = p_req7506.readedDetail.actNum;
        //设置时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        String dealedFile = getBankConfig().getValueAsString("DealedPath")+ File.separator+
                Format.Date8Format.format(calendar.getTime())+ File.separator+"MT942"+File.separator+accNum+"_7506.txt";
        //如果当天日间文件还没收到，就取前一天的日终文件，
        //如果前一天的日终文件没收到，说明是节假日，就继续往前遍历，最多10天
        int num =1;
        String day=getBankConfig().getValueAsString("countDay");//往前遍历天数
        while(!FileHelper.existFile(dealedFile)&&num<=Integer.parseInt(StringHelper.isNullOrEmpty(day) ?"10":day)){
            WriteBankLogLn("账户"+accNum+"在"+calendar+"日期的MT940文件不存在,继续往前一天遍历，第"+num+"次");
            calendar.add(Calendar.DAY_OF_MONTH,-1);
            dealedFile = getBankConfig().getValueAsString("DealedPath")+File.separator+
                    Format.Date8Format.format(calendar.getTime())+File.separator+
                    "MT940"+File.separator+accNum+"_7505.txt";
            num++;
        }
        if(!FileHelper.existFile(dealedFile)){
            detail.respCode ="0";
            detail.respInfo="此账号余额暂时无法获取";
        }
        //读取文件内容
        String balanceContent = FileHelper.readFileText(dealedFile,getCharset());
        WriteBankLogLn("读取到的文件内容："+balanceContent);
        if(StringHelper.hasAnyChar(balanceContent)){
            //账号|币种|当前余额|可用余额|昨日余额(MT940)|页码|
            //账号|币种|当前余额|可用余额|期初余额|(MT942)
            String[] bal  =balanceContent.split("\\|");
            detail.respCode ="0";
            detail.actNum =bal[0].trim();
            detail.actCur =convertCurToATS(bal[1].trim());
            detail.currBal =bal[2].trim();
            detail.availBal =bal[3].trim();
            detail.yesterdayBal = bal[4].trim();
        }else {
            detail.respCode="-1";
            detail.respInfo="文件读取异常";
        }
        retAts.writeRetDetail(detail,p_req7506.isBatch);
    }
        retAts.writeEnd();
        return retAts;
}

    /**
     * 历史查询 MT940
     */
    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        //查询时间校验
        String[] dateAry=getQueryDate(p_req7505.dateFrom,p_req7505.dateTo);
        p_req7505.dateFrom=dateAry[0];
        p_req7505.dateTo=dateAry[1];

        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try{
            String accNum = p_req7505.actNum;
            Date dateStart = Format.Date8Format.parse(p_req7505.dateFrom);
            Date dateEnd = Format.Date8Format.parse(p_req7505.dateTo);
            Calendar calendar=Calendar.getInstance();
            while (!dateStart.after(dateEnd)){
                String dealedPath = getBankConfig().getValueAsString("DealedPath")+File.separator+
                        Format.Date8Format.format(dateStart)+File.separator+"MT940"+File.separator+
                        accNum+"_7505.txt";
                if(!FileHelper.existFile(dealedPath)){
                    calendar.setTime(dateStart);
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                    dateStart = calendar.getTime();
                    continue;
                }
                //读取文件内容
                String balanceContent = FileHelper.readFileText(dealedPath,getCharset());
                WriteBankLogLn("读取到的文件内容："+balanceContent);
                if(StringHelper.hasAnyChar(balanceContent)){
                    //账号|币种|当前余额|可用余额|昨日余额(MT940)|页码
                    String[] bal  =balanceContent.split("\\|");
                    RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                    tmpRetDetail.actBal=bal[2].trim();
                    tmpRetDetail.availBal=bal[3].trim();
                    tmpRetDetail.balDate= Format.Date10Format.format(dateStart);
                    retAts.writeRetDetail(tmpRetDetail);
                }else {
                    tmpRespCode="-1";
                    tmpRespInfo="文件读取异常";
                }
                calendar.setTime(dateStart);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateStart = calendar.getTime();
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
